package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.AppDownload;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.AppDownloadService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @Author: WangXing
 * @Description:
 * @CreateDate: 2018/8/31 16:17
 */
@RestController
@RequestMapping(value = ManagerVersion.API_V1_PREFIX+"appDownload/")
@Api(value = "下载地址")
public class AppDownloadController {

    @Autowired
    private AppDownloadService appDownloadService;
    @Autowired
    private RedisServer redisServer;
    /**
     * 下载地址列表页面
     * @return
     */
    @GetMapping("dlManage")
    public Object dlManage(){
        return new ModelAndView("appDownload/dlPage");
    }

    /**
     * 查询下载地址列表
     * @param appDownload
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "查询app下载地址列表",notes = "查询app下载地址列表")
    @PostMapping("selDlList")
    public Object selDlList(AppDownload appDownload,
                            @RequestParam("page") Integer pageIndex,
                            @RequestParam("limit")Integer pageSize){
        PageInfo<AppDownload> info = appDownloadService.selectByEntityLists(appDownload,pageIndex,pageSize);
        return new ReturnResult<AppDownload>(0,"成功",info.getList(),info.getTotal());
    }

    /**
     * 新增下载地址页面
     * @return
     */
    @GetMapping("addDl")
    public Object addDl(){
        return new ModelAndView("appDownload/addDlPage");
    }

    /**
     * 新增下载地址
     * @param appDownload
     * @return
     */
    @ApiOperation(value = "新增app下载地址",notes = "查询app下载地址列表")
    @PostMapping("insDl")
    public Object insDl(AppDownload appDownload){
        redisServer.set(RedisKeysUtils.LOGINNAME,"新增app下载地址");
        AppDownload ol = new AppDownload();
        ol.setAppType(appDownload.getAppType());
        ol.setDownloadUrl(appDownload.getDownloadUrl());
        ol.setEnableFlag(true);
        AppDownload download = appDownloadService.selectByEntity(ol);
        if(null != download){
            return new ReturnResult<AppDownload>(-1,"重复添加",1,1);
        }
        appDownload.setCreateTime(new Date());
        appDownload.setEnableFlag(false);
        appDownloadService.insertSelective(appDownload);
        return new ReturnResult<AppDownload>(0,"成功",1,1);
    }

    /**
     * 编辑页面
     * @param id
     * @return
     */
    @GetMapping("editDl")
    public Object editDl(@RequestParam("id") Integer id){
        AppDownload download = appDownloadService.selectByPrimaryKey(id);
        ModelAndView mv = new ModelAndView("appDownload/editDlPage");
        mv.addObject("download",download);
        return mv;
    }

    /**
     * 编辑方法
     * @param appDownload
     * @return
     */
    @ApiOperation(value = "编辑app下载地址",notes = "编辑app下载地址列表")
    @PostMapping("updateDl")
    public Object updateDl(AppDownload appDownload){
        redisServer.set(RedisKeysUtils.LOGINNAME,"编辑app下载地址");
        AppDownload old = appDownloadService.selectByPrimaryKey(appDownload.getId());
        appDownload.setUpdateTime(new Date());
        appDownloadService.updateByPrimaryKeys(appDownload,old);
        return new ReturnResult<AppDownload>(0,"成功",1,1);
    }

    /**
     * 启用停用
     * @param appDownload
     * @return
     */
    @ApiOperation(value = "修改app下载地址状态",notes = "修改app下载地址状态")
    @PostMapping("enableDl")
    public Object enableDl(AppDownload appDownload){
        redisServer.set(RedisKeysUtils.LOGINNAME,"app下载地址启用/停用");
        List<AppDownload> list = appDownloadService.selectByTypeList(appDownload);
        if(null != list){
            for (int i = 0; i < list.size(); i++) {
                AppDownload sel = list.get(i);
                if(sel.getId().equals(appDownload.getId())){
                    appDownload.setUpdateTime(new Date());
                    if(null != appDownload.getEnableFlag()&& appDownload.getEnableFlag()){
                        appDownload.setEnableFlag(false);
                    }else{
                        appDownload.setEnableFlag(true);
                    }
                    appDownloadService.updateByPrimaryKeys(appDownload,sel);
                }else{
                    sel.setEnableFlag(false);
                    sel.setUpdateTime(new Date());
                    sel.setAppType(appDownload.getAppType());
                    sel.setId(appDownload.getId());
                    appDownloadService.updateByEntity(sel);
                }
            }
        }
        return new ReturnResult<AppDownload>(0,"成功",1,1);
    }
}
